package com.gofaraway.web.controller;

import com.gofaraway.service.facade.request.HelloRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ChenPeng
 * @Date 2024/1/18 14:21
 */
@RestController
@RequestMapping("/health")
public class HealthCheckController {

    private String status;
    public static final String LOCALHOST_IP = "127.0.0.1";
    public static final String STATUS_OFF = "off";
    public static final String STATUS_ON = "on";

    @GetMapping("/on")
    public String on(@RequestHeader("AUTH_CPAAS") String auth, HttpServletRequest request) {
        if (LOCALHOST_IP.equals(request.getRemoteAddr())) {
            this.status = STATUS_ON;
        }
        return this.status;
    }

    @GetMapping("/off")
    public String off(@RequestHeader("AUTH_CPAAS") String auth, HttpServletRequest request) {
        if (LOCALHOST_IP.equals(request.getRemoteAddr())) {
            this.status = STATUS_OFF;
        }
        return this.status;
    }

    @RequestMapping("/check")
    public ResponseEntity check() {
        if (STATUS_OFF.equals(this.status)) {
            return new ResponseEntity(HttpStatus.SERVICE_UNAVAILABLE);
        }
        return new ResponseEntity(HttpStatus.OK);
    }

    @PostMapping("/hello")
    public String hello(@RequestBody HelloRequest request) {
        return request.getHello();
    }
}
